//
//  SevenWeatherTableViewCell.m
//  fultureweather
//
//  Created by 王展 on 15/11/5.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "SevenWeatherTableViewCell.h"
#import "DrawLine.h"



@implementation SevenWeatherTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSevenView];
        [self createImageViewN];
        self.did = NO;
    }
    return self;
}

-(void)createSevenView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(60 * XWIDTH, 410 * XHIGHT);
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.sevenCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * XHIGHT, WIDTH + 0 * XWIDTH, 410 * XHIGHT) collectionViewLayout:flowLayout];
    self.sevenCV.contentSize =CGSizeMake(500, 0);
    self.sevenCV.delegate = self;
    self.sevenCV.dataSource = self;
    self.sevenCV.backgroundColor = [UIColor clearColor];
    self.sevenCV.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.sevenCV];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sevenArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.sevenCV registerClass:[SevenCollectionViewCell class] forCellWithReuseIdentifier:@"sevenCV"];
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"sevenCV" forIndexPath:indexPath];
    
    self.cell.seven = [self.sevenArray objectAtIndex:indexPath.row];
    
    return self.cell;
}

-(void)setSevenArray:(NSArray *)sevenArray
{
    _sevenArray = sevenArray;
    [self.sevenCV reloadData];
    
    //防止滑动TV时重用问题即防止多次画线
    if (self.did == NO) {
        [self drawLine];
        self.did = YES;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    //将cell改为透明的方法
    self.backgroundColor = [UIColor clearColor];
}


-(void)createImageViewN
{
    self.imageViewN = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 500, self.sevenCV.frame.size.height)];
    [self.sevenCV addSubview:self.imageViewN];
}

-(void)drawLine
{
    //画高温和低温线
    for (int i = 0; i < self.sevenArray.count - 1; i++) {
        Seven *seven1 = [self.sevenArray objectAtIndex:i];
        Seven *seven2 = [self.sevenArray objectAtIndex:i + 1];
        CGFloat high1 = [seven1.temp_high floatValue];
        CGFloat high2 = [seven2.temp_high floatValue];
        
        
        [DrawLine drawCenter:self.imageViewN withBeginX:30 * XWIDTH + (60 * XWIDTH + 10) * i withBeginY:170 * XHIGHT - high1 * 2.25 withEndX:30 * XWIDTH + (60 * XWIDTH + 10) *
         (i + 1) withEndY:170 * XHIGHT - high2 * 2.25 withLineWidth:2 * XWIDTH withColorR:1 G:1 B:1 Alpha:1];
    }
    
    for (int i = 0; i < self.sevenArray.count - 1; i++) {
        Seven *seven1 = [self.sevenArray objectAtIndex:i];
        Seven *seven2 = [self.sevenArray objectAtIndex:i + 1];
        CGFloat high1 = [seven1.temp_low floatValue];
        CGFloat high2 = [seven2.temp_low floatValue];
    
        
        [DrawLine drawCenter:self.imageViewN withBeginX:30 * XWIDTH + (60 * XWIDTH + 10) * i withBeginY:240 * XHIGHT - high1 * 2.25 withEndX:30 * XWIDTH + (60 * XWIDTH + 10) *
         (i + 1) withEndY:240 * XHIGHT - high2 * 2.25 withLineWidth:2 * XWIDTH withColorR:1 G:1 B:1 Alpha:1];
    }
    
}

@end
